<?php
/**
 * Created by PhpStorm.
 * User: dongxiaofeng
 * Date: 2017/3/16
 * Time: 15:56
 */
namespace zhtfrontend\modules\sitepadapi\controllers;

use common\components\helpers\Fun;
use common\models\datas\Message;
use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\data\ArrayDataProvider;
use yii\db\Query;
use yii\filters\Cors;
use yii\web\Response;

class MessageController extends ActiveController
{
    public $modelClass = 'common\models\datas\message';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::className(),
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Allow-Origin' => ['*'],
                'Access-Control-Request-Method' => ['POST', 'PUT', 'GET', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 3600,
                'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
            ],
        ];
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionList($customer_id,$receive_type = 'teacher'){
        $return = [];
        $site_id = Fun::getSiteId();
        //这时候的customer_id已经不是会员id了，而是admin_id，而admin又分为管理员 老师和班主任三种身份
        /**$admin = (new Query())->select('*')->from('%admin')->where(['id'=>$customer_id])->one(Fun::getSiteDb());
        switch ($admin['role']){
            case 'class_teacher':
                break;
            case 'teache_teacher':
                break;
            case 'manager':
                break;
        }*/
        if($receive_type == 'header'){
            $receive_type = 'class_teacher';
        }elseif ($receive_type == 'manager'){
            $receive_type = 'manager';
        }else{
            $receive_type = 'teacher';
        }
        $message = Message::find()->where(['receiver_id'=>$customer_id,'site_id'=>$site_id,'send_group'=>$receive_type])->andWhere(['in','status',[0,1]])->all();

        $types = \Yii::$app->params['type'];
        if($message != null){
            foreach ($message as $msg){
                $arr = [];
                $arr['id'] = $msg->id;
                $arr['title'] = $types[$msg->type_id]['name'];
                $arr['content'] = strip_tags($msg->content);
                $arr['type_id'] = $msg->type_id;
                $arr['created_at'] = \Yii::$app->formatter->asDatetime($msg->created_at);
                $arr['status'] = $msg->status;
                $return[] = $arr;
            }
        }


       // return ['statusCode'=>200,'message'=>'success','items'=>$return];
        return new ArrayDataProvider([
            'allModels' => $return,
            'pagination' => [
                'pageSize' => 6
            ],
        ]);

    }

    public function actionInfo($msg_id){
        $info = Message::findOne($msg_id);
        $arr = [];
        $types = \Yii::$app->params['type'];
        if($info != null) {
            $arr['id'] = $info->id;
            $arr['title'] = $types[$info->type_id]['name'];
            $arr['content'] = $info->content;
            $arr['created_at'] = \Yii::$app->formatter->asDatetime($info->created_at);
            $arr['type_id'] = $info->type_id;
        }
        return ['statusCode'=>200,'message'=>'success','items'=>$arr];

    }

    public function actionTypes(){
        $types = \Yii::$app->params['type'];
        $arr = [];
        foreach ($types as $key=>$type){
            $res['id'] = $key;
            $res['name'] = $type['name'];
            $arr[] = $res;
        }
        return ['statusCode'=>200,'message'=>'success','items'=>$arr];
    }

    public function actionChangestatus($message_id,$status){
        if(!empty($message_id) && in_array($status,[0,1,2])){
            \Yii::$app->db->createCommand()->update('{{%message}}', ['status' => $status], ['id'=>$message_id])->execute();
            return ['statusCode'=>200,'message'=>'success','items'=>'success'];
        }else{
            return ['statusCode'=>200,'message'=>'error','items'=>'error'];
        }
    }

}